package com.gl.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Date;

/**
 * Created by DELL on 2017/12/7.
 */
@Service
public class ConsumerService {
    @Autowired
    private RestTemplate restTemplate;

/*    @Autowired
    private LoadBalancerClient loadBalancerClient;*/

    @HystrixCommand(fallbackMethod = "findDate")
    public String getDate() {
/*        ServiceInstance serviceInstance = this.loadBalancerClient.choose("hello-service");
        System.out.println("===" + ":" + serviceInstance.getHost() + ":" + serviceInstance.getServiceId() + ":"
                + serviceInstance.getPort());*/

        return restTemplate.getForObject("http://hello-service/hello/date", String.class);
    }

    public String findDate(){
        return "service error , this is default date:" + new Date();
    }
}
